/*
 *Author: basanas
 */
 $.ajaxSetup({ cache: false });

var error_str = "";
var warning_str = "";

function validateFields()
{
    validateUsername();
    validatePasswords();
    validateEmails();
    validateCaptcha();

    if (error_str == "" && warning_str == "") document.register.submit();
    else if (error_str != "" && warning_str == "")
    {
        $("#msg-box").html("<div class=\"msgboxError prc50\"><ul>" + error_str + "</ul></div>");
        error_str = "";
    }
    else if (error_str == "" && warning_str != "")
    {
        $("#msg-box").html("<div class=\"msgboxWarning prc50\"><ul>" + warning_str + "</ul></div>");
        warning_str = "";
    }
    else
    {
        $("#msg-box").html("<div class=\"msgboxError prc50\"><ul>" + error_str + "</ul></div>" + "<div class=\"msgboxWarning prc50\"><ul>" + warning_str + "</ul></div>");
        error_str = "";
        warning_str = "";
    }
}

function validateUsername()
{
    var str = document.forms["register"]["username"].value
    if (str == null || str == "")
    {
        error_str = error_str + "<li>Username must be filled out</li>";
        return false;
    }

    //var regex=/^[A-Za-z][0-9A-Za-z_]+$/;
    var regex = /^[\w][\d\w]{2,}/;
    if (str.length < 3)
    {
        error_str = error_str + "<li>Username must be at least 3 characters long</li>";
        return false;
    }
    else if (!regex.test(str))
    {
        error_str = error_str + "<li>Username contains invalid characters (No special characters and spaces permitted)</li>";
        return false;
    }
    return true;
}

function validateCaptcha()
{
    var x = document.forms["register"]["recaptcha_response_field"].value
    if (x == null || x == "")
    {
        error_str = error_str + "<li>Captcha field must be completed</li>";
        return false;
    }
    return true;
}

function validatePasswords()
{
    var x = document.forms["register"]["password"].value
    if (x == null || x == "")
    {
        error_str = error_str + "<li>Password must be filled out</li>";
        return false;
    }

    var y = document.forms["register"]["password2"].value
    if (y == null || y == "")
    {
        warning_str = warning_str + "<li>Password must be confirmed</li>";
        return false;
    }
    else if (x != y)
    {
        warning_str = warning_str + "<li>Passwords don't match.</li>";
        return false;
    }
    return true;
}

function validateEmails()
{

    var x = document.forms["register"]["email"].value
    if (x == null || x == "")
    {
        error_str = error_str + "<li>Email must be filled out</li>";
        return false;
    }

    var regex = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/;
    if (!regex.test(x))
    {
        error_str = error_str + "<li>Email is not in the correct form</li>";
        return false;
    }

    var y = document.forms["register"]["email2"].value
    if (y == null || y == "")
    {
        warning_str = warning_str + "<li>Email must be confirmed</li>";
        return false;
    }
    else if (x != y)
    {
        warning_str = warning_str + "<li>Emails don't match.</li>";
        return false;
    }

    return true;
}